﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading;

namespace CircuitBreaker
{
    public static class CircuitBreakerFactory
    {
        static ConcurrentDictionary<String, CircuitBreaker> _circuitBreakerMap;
        static ConcurrentDictionary<String, CircuitBreakerSettings> _settingsMap;
         static CircuitBreakerFactory()
        {
            _circuitBreakerMap = new ConcurrentDictionary<string, CircuitBreaker>();
            _settingsMap = new ConcurrentDictionary<string, CircuitBreakerSettings>();
        }
        public static CircuitBreaker GetCircuitBreaker( CircuitBreakerSettings settings)
        {
            //CircuitBreaker circuitBreaker;
            //if (!_circuitBreakerMap.TryGetValue(settings.Name,out circuitBreaker))
            //{
            //    circuitBreaker = new CircuitBreaker(settings);
            //    if (_circuitBreakerMap.TryAdd(settings.Name,circuitBreaker))
            //    {
            //        circuitBreaker.Initialize();
            //    }
            //    else
            //    {
            //        _circuitBreakerMap.TryGetValue(settings.Name, out circuitBreaker);
            //    }
            //}

            CircuitBreaker circuitBreaker = _circuitBreakerMap.GetOrAdd(settings.Name, (k) => new CircuitBreaker(settings));
            return circuitBreaker;
        }
    }
}
